package com.xuxueli.crawler.test;

import com.xuxueli.crawler.XxlCrawler;
import com.xuxueli.crawler.annotation.PageFieldSelect;
import com.xuxueli.crawler.annotation.PageSelect;
import com.xuxueli.crawler.conf.XxlCrawlerConf;
import com.xuxueli.crawler.parser.PageParser;
import com.xuxueli.crawler.proxy.ProxyMaker;
import com.xuxueli.crawler.proxy.strategy.RoundProxyMaker;
import com.xuxueli.crawler.test.entity.Cart;
import com.xuxueli.crawler.test.entity.Category;
import com.xuxueli.crawler.test.util.HibernateUtil;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;

import java.net.InetSocketAddress;
import java.net.Proxy;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/*
 *  lcx 爬取所有商品的详细信息
 * */
public class AllcartDetail {

    @PageSelect(cssQuery = "body")
    public static class PageVo {

        @PageFieldSelect(cssQuery = "a.itemLink.block", selectType = XxlCrawlerConf.SelectType.TEXT)
        private List<String> shopNameList;

        @PageFieldSelect(cssQuery = "a.itemLink.block", selectType = XxlCrawlerConf.SelectType.ATTR, selectVal = "href")
        private List<String> shopNameListHref;

        @PageFieldSelect(cssQuery = "span.itemPrice", selectType = XxlCrawlerConf.SelectType.TEXT)
        private List<String> shopPriceListHref;

        @PageFieldSelect(cssQuery = ".warning > .clearMargin-bottom > li", selectType = XxlCrawlerConf.SelectType.TEXT)
        private String endState;



        @Override
        public String toString() {
            return "PageVo{" +
                    "shopNameList='" + shopNameList + '\'' +
                    ", shopNameListHref=" + shopNameListHref + '}';
        }
    }

    public static void main(String[] args) {

        int j = 0;

        Map headerMap = new HashMap();
        Map cookieMap = new HashMap();
        cookieMap.put("BUYER_CITY_SELECTED","80");


        headerMap.put("Cache-Control", "max-age=0");

        String[] ip = {
                "222.85.39.159:38939",
                "49.85.5.17:28885"
        };

        final List<PageVo> pageVoList = new ArrayList<>();
        final String[] link = new String[500];


        // 设置代理池
        ProxyMaker proxyMaker = new RoundProxyMaker()
                .addProxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress("183.163.40.223", 31773)));

        //查询需要爬取得类目
        HibernateUtil hibernateUtil = new HibernateUtil();
        List<Cart> cartList = hibernateUtil.selectAllCartDetailByAllcart();


        for (Cart cart : cartList) {


            j++;
            link[j] = cart.getCartHtrf();
        }

        XxlCrawler crawler = new XxlCrawler.Builder()
                .setUrls(link)
                .setThreadCount(20).setPauseMillis(1000)
                .setAllowSpread(false)
                //.setProxyMaker(proxyMaker)
                .setCookieMap(cookieMap)
                .setHeaderMap(headerMap)
                .setPageParser(
                        new PageParser<PageVo>() {
                            HibernateUtil hibernateUtil = new HibernateUtil();

                            @Override
                            public boolean parse(Document html, Element pageVoElement, PageVo pageVo) {
                                // 解析封装 PageVo 对象
                                System.out.println("----------------------------------------------------------");
                                System.out.println("-------" + pageVo.toString());
                                pageVoList.add(pageVo);
                                System.out.println("---------------------------------------------------------");
                                //hibernateUtil.saveCart(pageVo);

                                return true;
                            }
                        }
                )
                .build();
        System.out.println("start");
        crawler.start(true);
        System.out.println("end");

    }


}
